/*
 * StockExperiments.java
 * 
 * Created on Mar 21, 2009, 11:41:00 PM
 * 16, October, 2009: Class normalized along with the classes ExcludeColumns, ExcludeTables, InputAttribs, TimeLine and StockExperiments in order to allow the creation of an
 * abstract class AttributeFabric, in order to be conformant with design pattern method.
 * 06, April, 2011: Base class no longer inherits from class Table, rather table is now a composition object.
 * 18, November, 2011: Class modified to use TableFactory.
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package portfolio.dbHost.project.attributes;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import portfolio.dbHost.Host;
import portfolio.dbHost.dataBases.dbTable.MysqlTable;
import portfolio.dbHost.dataBases.dbTable.OdbcTable;
import portfolio.dbHost.dataBases.dbTable.Table;
import portfolio.dbHost.dataBases.dbTable.TableFactory;
import portfolio.dbHost.project.Project;
import portfolio.dbHost.project.attributes.experiments.Experiment;

/**
 *
 * @author ACA
 */
public class StockExperiments /*extends Table*/ implements AbstractAttribute, MouseListener
{
    private Table table = null;

    static final public String TABLE_NAME            = "stockexperiments";
    static final public String COLUMN_EXPERIMNETNAME = "name";
    static final public String COLUMN_VARIABLES      = "variables";
    static final public String COLUMN_SVMCONFIG      = "svmconfig";
    static final public int    EXPERIMENTNAME_LEN    = 60;
    static final public String TYPE_EXPERIMENTNAME   = COLUMN_EXPERIMNETNAME + " CHAR (" + EXPERIMENTNAME_LEN +")";
    static final public String TYPE_VARIABLES        = COLUMN_VARIABLES      + " BLOB";
    static final public String TYPE_SVMCONFIG        = COLUMN_SVMCONFIG      + " BLOB";
    
    private Vector             vStockExperiments     = null;
    private Experiment []      experimentsArray      = null;
    
    /**
     * @Overrides parent's method Table.mousePressed 
     */
    int mouseReleased = 1;
    public void mouseClicked(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": StockExperiments:   ------------------------- Mouse Clicked -------------------------");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": StockExperiments:  ------------------------- Mouse Pressed -------------------------");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": StockExperiments:   ------------------------- Mouse Released ------------------------");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": StockExperiments:   ------------------------- Mouse Entered -------------------------");
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        String sClassName = e.getClass().getName();
        System.out.println (mouseReleased++ + " className: " + sClassName + ": StockExperiments:   ------------------------- Mouse Exited  -------------------------");
//        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    /** Creates a new instance of ExcludeTables 
     * @param projectParam 
     * @throws java.lang.Exception 
     */
    public StockExperiments (Project projectParam) throws Exception
    {
////        super (projectParam);
////        super.setName(TABLE_NAME);
//        int iDBType = projectParam.getDBHost().getDBType();
//        if (iDBType == Host.DB_TYPE_MYSQL) {
//            table = new MysqlTable (projectParam);
//        } else if (iDBType == Host.DB_TYPE_ODBC) {
//            table = new OdbcTable (projectParam);
//        } else {
//            throw new Exception ("Unknown Data Base.");
//        }
        TableFactory tableFactory = new TableFactory ();
        table = tableFactory.getDBType(projectParam.getProjectDBase());
        table.setName(StockExperiments.TABLE_NAME);
        
        try {
            table.describe();
        } catch (Exception e) {
            this.create ();
        }
    }
    /**
     *
     *
     */
    public void initComponents () throws Exception
    {
        vStockExperiments = this.getStockExperimentsVector();
        experimentsArray  = this.createExperimentsNodes(vStockExperiments);

        table.buildColumns();
    }
    /**
     * 
     * @return 
     */
    public ResultSet getQuery () throws Exception 
    {
        return table.getQuery(StockExperiments.COLUMN_EXPERIMNETNAME);

    }
    /**
     * 
     */
    private Vector getStockExperimentsVector () throws Exception 
    {
        
        Vector vTemp = new Vector ();
        
        ResultSet resultSet = this.getQuery ();

        if (resultSet.last()) {
            resultSet.first();
        
            do {
                String sNameColumn = resultSet.getString(this.COLUMN_EXPERIMNETNAME);
                vTemp.add(sNameColumn);
            } while (resultSet.next());
        } else {
            //System.out.println ("Warning: No experiments to show.");
        }
                
        return vTemp;
    }
    /**
     * 
     */
    private Experiment [] createExperimentsNodes (Vector vExperimentsParam) throws Exception 
    {
        
        int iStockExperimentsLen = vExperimentsParam.size();
        if (iStockExperimentsLen != 0) {
            Experiment [] experimentsTmp = new Experiment [iStockExperimentsLen];
            for (int iIdx = 0; iIdx < iStockExperimentsLen; iIdx++) {
                experimentsTmp [iIdx] = new Experiment (this, vExperimentsParam.get(iIdx).toString());
            }

            return experimentsTmp;
        }
        return null;
    }
    /**
     *
     */
    public DefaultMutableTreeNode processHierarchy ( ) //throws Exception 
    {
        try {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode (TABLE_NAME);

            DefaultMutableTreeNode child = null;

            vStockExperiments = this.getStockExperimentsVector();
            experimentsArray  = this.createExperimentsNodes(vStockExperiments);

            if (experimentsArray == null)
                return node;

            if (experimentsArray.length <= 0) //0j0
                return node;

            for (int i = 0; i < experimentsArray.length; i++) {
                child = experimentsArray [i].processExperimentHierarchy();
                node.add (child);
            }
            return node;
        } catch (Exception e) {
            table.getDBase().getDBHost().getMainScreen().displayMessage("StockExperiments.processHierarchy 146", e.toString());
        }
        return null;
    }
    /**
     *
     * @param dbTreeParam 
     * @param iXPos 
     * @param iYPos 
     */
     public void getPopupOptions (JTree dbTreeParam,  int iXPos, int iYPos)
    {
        JTree dbaseTree = dbTreeParam;
        
        JPopupMenu treeOptionsMenu = new JPopupMenu ("Options");
        dbaseTree.add(treeOptionsMenu);
        
        treeOptionsMenu.addMouseListener(this);
        
        treeOptionsMenu.setLocation (iXPos, iYPos);
        treeOptionsMenu.show (treeOptionsMenu, iXPos, iYPos);
        
        return;
    } /* end method getPopupOptions */ 
    /**
     * Returns StockExperiments list, in a String array of n rows, distributed as follows:
     * N rows and one column containing a list of ExperimentName.
     */
    public String [] get ()
    {
        String [] sExcludeTables = (String [])vStockExperiments.toArray(new String [0]);
        
        return sExcludeTables;
    }
    /*
     * 
     */
    public Object getTreeObject (Object [] oPathParam)
    {
        if (oPathParam == null) {
            return null;
        }
        if (oPathParam.length <2) {
            return null;
        }
        if (!oPathParam [1].toString().equals(this.TABLE_NAME)) {
           return null;  //error, no database found
        }
        
        if (oPathParam.length != 2) {
            String sExperiment = oPathParam [2].toString();
            int iIdx = vStockExperiments.indexOf(sExperiment);
            if (iIdx != -1) {
                return experimentsArray[iIdx].getTreeObject (oPathParam);
            } else {
                //error, no database found
                return null;
            }
        }
        return this;
    } /* end method getTreeObject */
    /**
     * 
     */
    public void create () throws Exception
    {
        table.create (this.TYPE_EXPERIMENTNAME + ", " + this.TYPE_VARIABLES + ", " + this.TYPE_SVMCONFIG +
                     ", INDEX (" + this.COLUMN_EXPERIMNETNAME +")");        
    }
    /**
     * 
     */
      public void insert (Vector vValuesParam) throws Exception 

    {
        Vector vColumns = new Vector ();
        vColumns.add(StockExperiments.COLUMN_EXPERIMNETNAME);
        vColumns.add(StockExperiments.COLUMN_VARIABLES);
        vColumns.add(StockExperiments.COLUMN_SVMCONFIG);

        String sStockExperiment = vValuesParam.get(0).toString();
        if (vStockExperiments.contains(sStockExperiment)) {
            throw new Exception ("Warning: There is already an experiment with the name " + sStockExperiment);
        }
System.out.println ("===========================> StockExperiments.insert");
        table.insert(vColumns, vValuesParam);
    }
    /**
     * 
     */
    static public Vector read (Connection connection, String sDBName)  throws Exception
    {
        throw new Exception ("Method not implemented");
    }
    /**
     * 
     */
    public Table getTable ()
    {
        return table;
    }
}
